package hw_8_3.thread_demo4;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Customer extends Thread{
    private static int balance = 0;
    private static final Lock lock = new ReentrantLock();
    private String name;

    public Customer(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            lock.lock();
            try {
                balance += 100;
                System.out.println(name + "存入100元，当前账户余额为：" + balance);
            } finally {
                lock.unlock();
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

